﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using chesslibrary.cs;
using System.Collections;
using System.Media;

namespace ChessGui
{
    public partial class fmain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public fmain()
        {
            InitializeComponent();
            thegame = new game_chess(new Manualplayer(color_pieces.White), new Manualplayer(color_pieces.Black));
            buttons = new Fields();
            images = new image_chess();
            init();
            thegame.StartGame += new ChessSimpleEvent(StartTheGame);
            thegame.EndGame += new ChessSimpleEvent(EndTheGame);
            thegame.MoveUndone += new ChessSimpleEvent(UndoTheMove);
            thegame.MoveExecuted += new ChessMoveEvent(MakeMove);
            thegame.Start();  
        }
        private Fields buttons;
        private cell_chess selectedcell;
        private game_chess thegame;
        private image_chess images;
        SoundPlayer sound = new SoundPlayer();
        private void PlaySound(string filePath)
        {
            sound.SoundLocation = filePath;
            try
            {
                sound.Load();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (sound.IsLoadCompleted)
                sound.Play();
            if (sound.IsLoadCompleted && filePath.Contains("bgmusic.wav"))
            {
                sound.PlayLooping();
            }
        }
       
        private void barButtonItemnewgame_ItemClick(object sender, ItemClickEventArgs e)
        {
            sound = new SoundPlayer();
            PlaySound(Application.StartupPath.Replace("bin\\Debug", "Sound\\bgmusic.wav"));
            thegame.Start();
        }

        private void barButtonItemthoat_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        private void barButtonItemUndo_ItemClick(object sender, ItemClickEventArgs e)
        {
            thegame.undo();
        }

        private void barButtonItemRude_ItemClick(object sender, ItemClickEventArgs e)
        {
            Help.ShowHelp(this, Application.StartupPath + "\\LuatCoVua.chm", HelpNavigator.TableOfContents);
        }

        private void buttonTacgia_ItemClick(object sender, ItemClickEventArgs e)
        {
            Form tg = new thong_tin();
            tg.Show();
        }
        private void init()
        {
            foreach (ChessButton b in buttons)
            {
                label1.Text = "A";
                label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label1.ForeColor = System.Drawing.Color.Yellow;
                label2.Text = "B";
                label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label2.ForeColor = System.Drawing.Color.Yellow;
                label3.Text = "C";
                label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label3.ForeColor = System.Drawing.Color.Yellow;
                label4.Text = "D";
                label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label4.ForeColor = System.Drawing.Color.Yellow;
                label5.Text = "E";
                label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label5.ForeColor = System.Drawing.Color.Yellow;
                label6.Text = "F";
                label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label6.ForeColor = System.Drawing.Color.Yellow;
                label7.Text = "G";
                label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label7.ForeColor = System.Drawing.Color.Yellow;
                label8.Text = "H";
                label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label8.ForeColor = System.Drawing.Color.Yellow;
                label9.Text = "H";
                label9.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label9.ForeColor = System.Drawing.Color.Yellow;
                label10.Text = "G";
                label10.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label10.ForeColor = System.Drawing.Color.Yellow;
                label11.Text = "F";
                label11.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label11.ForeColor = System.Drawing.Color.Yellow;
                label12.Text = "E";
                label12.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label12.ForeColor = System.Drawing.Color.Yellow;
                label13.Text = "D";
                label13.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label13.ForeColor = System.Drawing.Color.Yellow;
                label14.Text = "C";
                label14.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label14.ForeColor = System.Drawing.Color.Yellow;
                label15.Text = "B";
                label15.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label15.ForeColor = System.Drawing.Color.Yellow;
                label16.Text = "A";
                label16.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label16.ForeColor = System.Drawing.Color.Yellow;
                label17.Text = "1";
                label17.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label17.ForeColor = System.Drawing.Color.Yellow;
                label18.Text = "2";
                label18.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label18.ForeColor = System.Drawing.Color.Yellow;
                label19.Text = "3";
                label19.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label19.ForeColor = System.Drawing.Color.Yellow;
                label20.Text = "4";
                label20.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label20.ForeColor = System.Drawing.Color.Yellow;
                label21.Text = "5";
                label21.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label21.ForeColor = System.Drawing.Color.Yellow;
                label22.Text = "6";
                label22.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label22.ForeColor = System.Drawing.Color.Yellow;
                label23.Text = "7";
                label23.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label23.ForeColor = System.Drawing.Color.Yellow;
                label24.Text = "8";
                label24.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label24.ForeColor = System.Drawing.Color.Yellow;
                label25.Text = "8";
                label25.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label25.ForeColor = System.Drawing.Color.Yellow;
                label26.Text = "7";
                label26.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label26.ForeColor = System.Drawing.Color.Yellow;
                label27.Text = "6";
                label27.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label27.ForeColor = System.Drawing.Color.Yellow;
                label28.Text = "5";
                label28.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label28.ForeColor = System.Drawing.Color.Yellow;
                label29.Text = "4";
                label29.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label29.ForeColor = System.Drawing.Color.Yellow;
                label30.Text = "3";
                label30.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label30.ForeColor = System.Drawing.Color.Yellow;
                label31.Text = "2";
                label31.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label31.ForeColor = System.Drawing.Color.Yellow;
                label32.Text = "1";
                label32.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label32.ForeColor = System.Drawing.Color.Yellow;
                panel_banco.Controls.Add(b);
            }
            buttons.onSelect += new ChessButtonHandler(buttonselect);
            buttons.onMove += new ChessButtonHandler(fieldmove);
        }
        protected void buttonselect(cell_chess cell)
        {
            selectedcell = cell;
            PlaySound(Application.StartupPath.Replace("bin\\Debug", "Sound\\CardSelected.wav"));
            ClearScreen();
            ArrayList l = thegame.GetValidMoves(cell);
            IEnumerator i = l.GetEnumerator();
            while (i.MoveNext())
            {
                properties_cell test = (properties_cell)i.Current;
                buttons[test].BackColor = Color.Yellow;
                if (buttons[test].BackColor != Color.Yellow)
                {
                    PlaySound(Application.StartupPath.Replace("bin\\Debug", "Sound\\Thua.wav"));
                }
            }
            buttons[selectedcell].BackColor = Color.DarkRed;
        }
        protected void fieldmove(cell_chess cell)
        {
            try
            {
                thegame.movePiece(selectedcell, cell);
                PlaySound(Application.StartupPath.Replace("bin\\Debug", "Sound\\Move.wav"));
                ClearScreen();
            }
            catch (ChessException e)
            {
                MessageBox.Show(e.ToString() + "\n at row " + e.Row + " and column " + e.Col);
            }
        }
        private void RefreshScreen()
        {
            foreach (ChessButton b in buttons)
            {
                pieces_chess p = thegame[b.Cell].pieces_chess;

                if (p == null)
                {
                    b.Image = null;
                }
                else
                {
                    if (p.IsOwnedBy(color_pieces.White))
                    {
                        b.ForeColor = Color.White;
                    }
                    else
                    {
                        b.ForeColor = Color.Black;
                    }

                    b.Image = images[p];
                }
            }
        }
        private void ClearScreen()
        {
            foreach (ChessButton b in buttons)
            {
                if (b.Cell.IsDark)
                {
                    b.BackColor = Color.Blue;
                }
                else
                {
                    b.BackColor = Color.DarkGreen;
                }
            }
        }
        private void StartTheGame()
        {
            RefreshScreen();
        }
        private void EndTheGame()
        {
            RefreshScreen();
        }
        private void UndoTheMove()
        {
            RefreshScreen();
        }
        private void MakeMove(move_pieces m)
        {
            RefreshScreen();
        }
    }
}